//
//  SearchResultViewController.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/9.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "SearchResultViewController.h"
#import "HomeBaseCell.h"
#import "HomeOneImageCell.h"
#import "NewsVideoCell.h"
#import "FBRequestSearch.h"

@interface SearchResultViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)FBRequestSearch *searchModel;
@property(nonatomic, strong)NSMutableArray *aticleArray;
@end

@implementation SearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchModel.StrKey = self.searchText;
    [self.view addSubview:self.tableView];
    self.aticleArray = [NSMutableArray array];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    WEAKSELF
    self.tableView.mj_header = [XMRefreshHeader xm_headerWithRefreshingBlock:^{
        [weakSelf searchAticle];
    }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)searchAticle{
    
    [FBNetworkTool get:@"articleapi/com.SearchAticle" parameters:self.searchModel.mj_keyValues showHUD:NO responseCache:^(XMHttpResponseModel *responseModel) {
        NSArray * array = [AticleModel mj_objectArrayWithKeyValuesArray:responseModel.data];
        [self.aticleArray addObjectsFromArray:array];
    } success:^(XMHttpResponseModel *responseModel) {
        [self.tableView.mj_header endRefreshing];
        if (responseModel.code == 200) {
            NSArray * array = [AticleModel mj_objectArrayWithKeyValuesArray:responseModel.data];
            [self.aticleArray addObjectsFromArray:array];
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.aticleArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    AticleModel * model = self.aticleArray[indexPath.row];
    
    if (model.ImgUrl.length > 0) {
        return [HomeOneImageCell heightWithModel:model];
    }
    return [HomeBaseCell heightWithModel:model];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeBaseCell * cell = [HomeBaseCell cellWithTableView:tableView];
    cell.model = self.aticleArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}








-(FBRequestSearch *)searchModel{
    if (!_searchModel) {
        _searchModel = [[FBRequestSearch alloc]init];
    }
    return _searchModel;
}

- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        _tableView = tableView;
    }
    return _tableView;
}

@end
